package com.ithe.canteen.service.impl;

import com.ithe.canteen.mapper.CommentMapper;
import com.ithe.canteen.mapper.UserMapper;
import com.ithe.canteen.pojo.Comment;
import com.ithe.canteen.service.CommentService;
import com.ithe.canteen.service.FoodService;
import com.ithe.canteen.service.OrderService;
import com.ithe.canteen.service.UserService;
import com.ithe.canteen.util.CreatIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Override
    public List<Comment> queryComment(String foodID) {
        List<Comment> comments = commentMapper.queryComment(foodID);
        for (Comment comment : comments) {
            String customerID = comment.getCommentUser();
            String nickname = userService.queryNickName(customerID);
            comment.setCommentUser(nickname);
        }
        return comments;
    }

    @Override
    public List<Comment> getCommentList() {
        return commentMapper.getCommentList();
    }

    @Override
    public boolean deleteComment(String commentID) {
        return commentMapper.deleteComment(commentID);
    }

    @Override
    public boolean insertComment(String orderID, String commentUser, String commentContent) {
        String commentFood = orderService.getFoodID(orderID);
        String commentID = CreatIDUtil.createID("CM");
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日", Locale.CHINA);
        String commentDate = currentDate.format(formatter);
        return commentMapper.insertComment(commentID, commentFood, commentUser, commentDate, commentContent);
    }

}
